// pages/addAddress/addAddress.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
    bindRegionChange(e){
      console.log(e.detail.value[0])
      this.setData({
          City: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
          province: e.detail.value[0],
          city: e.detail.value[1],
          area: e.detail.value[2]
      })
    },
  data: {
      region:['陕西省'],//默认省市区
    isShow:"none",
    isShow_1: "none",
    isShow_2: "none",
    display:"none",
    top:0,
    name_1:"选择市",
    name_2: "选择区",
    name_3: "选择社区",
    one: [
      {
        isSelect: false,
        citys: [
          { city_name: "陕西省" },
        ]
      },
    ],
    two: [
      {
        isSelect: false,
        areas: [
          { area_name: "西安市" },
        ]
      },
    ],
    three:[
      {
        isSelect:false,
        citys: [
          { city_name: "临潼区" },
          { city_name: "碑林区" },
          { city_name: "未央区" },
          { city_name: "长安区" },
          { city_name: "临潼区" },
          { city_name: "碑林区" },
          { city_name: "未央区" },
          { city_name: "长安区" },
        ]
      },
    ],
    City:'点击选择'
    
  },
  //获取表单值
  name:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  add: function (e) {
    this.setData({
      add: e.detail.value
    })
  },
  //表单提交
  subbtn:function(){
    let that = this;
    // 收货人 验证
    if (!that.data.name){
        wx.showToast({
            title: '收货人不能为空',
            icon: 'none'
        })
        return false;
    }
    //电话 验证
    let phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    var phone = this.data.phone;
    if (!phoneReg.test(phone)) {
        wx.showToast({
            title: '请输入有效的手机号码！',
            icon: 'none'
        }) 
        return false;
    }  
    if (this.data.City =='点击选择'){
        wx.showToast({
            title: '请选择省市区',
            icon: 'none'
        }) 
        return false;
    }
    wx.request({
      url: app.data.Url +'address/addlist',
      data:{
        uid:wx.getStorageSync('userInfo').uid,
        address:that.data.add,
        name:that.data.name,
        phone:that.data.phone,
        province: that.data.province,
        city: that.data.city,
        area: that.data.area,
        default: that.data.switch,
        // bid
        bid: wx.getStorageSync('bid')
      },
      method:'get',
      success:function(res){
        console.log(res)
        if(res.data.data.success==1){
          wx.showToast({
            title: '添加成功！',
          })
          console.log(111111111111111111111111111111)
          wx.navigateBack({
            delta: 2
          })
          var pages = getCurrentPages();
          if (pages.length > 1) {
              //上一个页面实例对象
              var prePage = pages[pages.length - 2];
              //关键在这里,这里面是触发上个界面
              prePage.get_address()
          }
        }
      }
    })
  },
  chose_city:function(e){
    var i = 0;
    let id = e.currentTarget.dataset.id,

      index = parseInt(e.currentTarget.dataset.index);
    this.data.one[0].isSelect = !this.data.one[0].isSelect;
    this.data.two[0].isSelect = !this.data.two[0].isSelect;
    if (this.data.one[0].isSelect){
      this.setData({
        isShow:"inline-block"
      })
    }
    else{
      this.setData({
        isShow: "none"
      })
    }
    this.setData({
      one: this.data.one,
      top:"50rpx",
    });
  },

  //默认地址按钮
  switchChange:function(e){
    this.setData({
      switch: e.detail.value?1:0
    })
  },


  chose_area: function (e) {
    var i = 0;
    let id = e.currentTarget.dataset.id,

      index = parseInt(e.currentTarget.dataset.index);
    this.data.two[0].isSelect = !this.data.two[0].isSelect;
    if (this.data.two[0].isSelect) {
      this.setData({
        isShow_1: "inline-block"
      })
    }
    else {
      this.setData({
        isShow_1: "none"
      })
    }
    this.setData({
      two: this.data.two,
      top: "50rpx",
    });
  },
  chose_community: function (e) {
    var i = 0;
    let id = e.currentTarget.dataset.id,

      index = parseInt(e.currentTarget.dataset.index);
    this.data.three[0].isSelect = !this.data.three[0].isSelect;
    if (this.data.three[0].isSelect) {
      this.setData({
        isShow_2: "inline-block"
      })
    }
    else {
      this.setData({
        isShow_2: "none"
      })
    }
    this.setData({
      three: this.data.three,
      top: "50rpx",
    });
  },
  has_chose_city:function(e){
    this.data.one[0].isSelect = !this.data.one[0].isSelect;
    var i = 0;
    let index = parseInt(e.currentTarget.dataset.index);
    console.log(this.data.one[0].citys[index].city_name,'-------')
    this.setData({
      name_1: this.data.one[0].citys[index].city_name,
      isShow: "none"
    })
  },
  has_chose_area: function (e) {
    this.data.two[0].isSelect = !this.data.two[0].isSelect;
    var i = 0;
    let index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      name_2: this.data.two[0].areas[index].area_name,
      isShow_1: "none"
    })
  },
  has_chose_community: function (e) {
    this.data.three[0].isSelect = !this.data.three[0].isSelect;
    var i = 0;
    let index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      name_3: this.data.three[0].citys[index].city_name,
      isShow_2: "none"
    })
  },
  chose_address:function(){
    this.setData({
      display:"block"
    })
  },
  hideview: function () {
    let that = this;
    //console.log(that.data.name_1 + '----' + that.data.name_1 + '----' + that.data.name_1)
    this.setData({
      province: that.data.name_1,
      city: that.data.name_2,
      area: that.data.name_2,



      City: that.data.name_1 + ',' + that.data.name_2 + ',' + that.data.name_2,
      display: "none"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})